package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import com.yanzhu.service.LikeService;
import com.yanzhu.service.VideoService;
import com.yanzhu.service.model.mix.VideoInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 19:39 2018/1/22
 * @Modified:
 */
@RestController
@PropertySource("classpath:config/config.properties")
public class VideoAPI {

    @Autowired
    private  VideoService videoService;

    @Autowired
    private  LikeService likeService;

    @Value("${view}")
    private int view;

    @RequestMapping(value="/queryUserVideoList")
    public ResponseBean queryVideoList(HttpServletRequest request){
        System.out.println("===================================================");
        String uid = request.getParameter("uid");
        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数！");

        String page = request.getParameter("page");
        if(page == null || page.equals(""))
            return ResponseBean.fail("缺少页数参数!");

        String size = request.getParameter("size");
        if(size == null || size.equals(""))
            return ResponseBean.fail("缺少页数大小参数!");

//        System.out.println(videoService.selectByUid(Long.parseLong(uid), Integer.parseInt(page), Integer.parseInt(size)));
        return ResponseBean.ok(videoService.selectByUid(Long.parseLong(uid), Integer.parseInt(page), Integer.parseInt(size)));
    }

    @RequestMapping(value="/queryHotVideo")
    public ResponseBean queryHotVideo(HttpServletRequest request){

        String page = request.getParameter("page");
        if(page == null || page.equals(""))
            return ResponseBean.fail("缺少页数参数!");

        String size = request.getParameter("size");
        if(size == null || size.equals(""))
            return ResponseBean.fail("缺少页数大小参数!");

//        System.out.println(videoService.selectByUid(Long.parseLong(uid), Integer.parseInt(page), Integer.parseInt(size)));
        return ResponseBean.ok(videoService.queryHotVideo(view, Integer.parseInt(page), Integer.parseInt(size)));
    }

    @RequestMapping(value="/queryVideoInfo")
    public ResponseBean queryVideoInfo(HttpServletRequest request){
        String vid = request.getParameter("vid");
        if(vid == null || vid.equals(""))
            return ResponseBean.fail("缺少视频ID参数！");

        String uid = request.getParameter("uid");
        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数！");

        VideoInfo videoInfo = videoService.queryVideoInfo(Long.parseLong(vid), Long.parseLong(uid));

        videoService.updateVideoView(Long.parseLong(vid));//更新视频浏览量

        return ResponseBean.ok(videoInfo);
    }

    @RequestMapping(value="/queryVideoById")
    public ResponseBean queryVideoById(HttpServletRequest request){
        String id = request.getParameter("vid");

        if(id == null || id.equals(""))
            return ResponseBean.fail("缺少ID参数！");

        return ResponseBean.ok(videoService.selectById(Long.parseLong(id)));
    }

    @RequestMapping(value="/deleteVideoById")
    public ResponseBean deleteVideoById(HttpServletRequest request){
        String id = request.getParameter("vid");

        if(id == null || id.equals(""))
            return ResponseBean.fail("缺少ID参数！");

        if(videoService.deleteVideoById(Long.parseLong(id)) < 0)
            return ResponseBean.fail("数据库错误！");

        return ResponseBean.ok();
    }

    @RequestMapping(value="/deleteAllUserVideo")
    public ResponseBean deleteAllVideo(HttpServletRequest request){
        String uid = request.getParameter("uid");

        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数！");

        if(videoService.deleteVideoAll(Long.parseLong(uid)) < 0)
            return ResponseBean.fail("数据库错误！");

        return ResponseBean.ok();
    }

}
